package com.css.apps.base.org.action;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.css.apps.base.org.model.SSysOrgTree;
import com.css.db.page.Page;
import com.css.db.query.QueryCache;
import com.css.util.Messages;
import com.css.util.StringHelper;
import com.css.webbase.webwork.action.AdminAction;
import com.opensymphony.xwork.Action;

/**
 * <p>
 * 查询自定义组织机构树分页列表
 * </p>
 * 
 */
@SuppressWarnings("serial")
public class DirCustom extends AdminAction {
	
	private static Log log = LogFactory.getLog(DirCustom.class);
	private String sysName;
	private String sysId;
	private String name;
	private Page page;

	public DirCustom() {
		page = new Page();
		page.setCountField("a.uuid");
	}

	@Override
	protected String adminGo() {
		try {
			QueryCache qc = new QueryCache(" select a.uuid from SSysOrgTree a  " + getWhere() + getOrder());
			setWhere(qc);
			page = qc.page(page);
			page.setResults(QueryCache.idToObj(SSysOrgTree.class, page.getResults()));
			return Action.SUCCESS;
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			setMessage(Messages.getString("systemMsg.exception"));
			return Action.ERROR;
		}
	}

	public String getWhere() {
		StringBuffer sb = new StringBuffer("where 1 = 1");
		if (StringHelper.isNotEmpty(name))
			sb.append(" and a.sysId like :name ");
		return sb.toString();
	}

	public String getOrder() {
		return StringHelper.isNotEmpty(page.getOrderByString()) ? page.getOrderByString() : "order by a.sysId";
	}

	public void setWhere(QueryCache qc) {
		if (StringHelper.isNotEmpty(name))
			qc.setParameter("name", "%" + name.trim() + "%");
	}

	public String getSysName() {
		return sysName;
	}

	public void setSysName(String sysName) {
		this.sysName = sysName;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public String getSysId() {
		return sysId;
	}

	public void setSysId(String sysId) {
		this.sysId = sysId;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}